
import os
import wx
import ab_document
import ab_file
import ab_text
import ab_viewer_panel

from ab_object_disp import ObjectDisp


class AsciiTableDocFrame(ab_document.DocumentFrame):
    def __init__(self, parent, app, hanlder, **kws):
        # set the doc as a private one
        ab_document.DocumentFrame.__init__(self, parent, app, hanlder,
                                           'Ascii Table')

        lines = list()
        lines.append('Chr Hex Dec ' * 8)
        for i in range(0, 256 / 8):
            lo = ''
            for j in range(0, 8):
                v = i + j * 256 / 8
                if 0x20 <= v < 0x7f:
                    c = chr(v)
                else:
                    c = ' '
                lo += ' %s   %02X %03d ' % (c, v, v)
            lines.append(lo)

        li = os.linesep.join([lo.rstrip() for lo in lines])

        tmpf = ab_file.File(app, None, "rb", data=li)
        _, spn = ObjectDisp.detectFileFormat(self.app, tmpf, forcetext=True)
        opaque = ab_viewer_panel.ViewerOpaque(None, tmpf, spn)
        text_ctrl = ab_text.TextViewerCtrl(self, app, opaque)

        sizer = wx.BoxSizer()
        sizer.Add(text_ctrl, 1, wx.EXPAND, 0)
        self.SetSizer(sizer)

        self.setReady()


class AsciiTableDocHandler(ab_document.DocumentHandler):
    def __init__(self):
        ab_document.DocumentHandler.__init__(self, 'ascii', AsciiTableDocFrame,
                                             True, False)
